home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / atoolbar.zip / DEMOTOOL.C < prev    next >
C/C++ Source or Header  |  1993-04-07  |  10KB  |  315 lines

  1. /*
  2.     DEMOTOOL.C -- program to demonstrate the abilities of
  3.         Tool Bar control implemented in ESTOOLS.DLL
  4.     Copyright (C) Eugene Sokolov 1992, (516)632-7892,
  5.     esokolov@sbchm1.chem.sunysb.edu
  6.  
  7.     You can freely copy, change or redistribute this file as long
  8.     as this notice remains intact.
  9. */
  10.  
  11. #define STRICT    
  12.  
  13. #include <windows.h>
  14. #include "esdefs.h"
  15.  
  16. LRESULT CALLBACK     MainWndProc    ( HWND, UINT, WPARAM, LPARAM );
  17. extern HWND FAR PASCAL     CreateToolBar    ( HINSTANCE, LPSTR, HWND, POINT );
  18.  
  19. HINSTANCE hInst;
  20.  
  21. BOOL InitApplication(HINSTANCE hInstance)
  22. {
  23.     WNDCLASS  wc;
  24.  
  25.     wc.style     = CS_HREDRAW | CS_VREDRAW;
  26.     wc.lpfnWndProc     = (long (FAR PASCAL*)())MainWndProc;
  27.                                                         // windows of this class.
  28.     wc.cbClsExtra         = 0;    
  29.     wc.cbWndExtra         = 0;    
  30.     wc.hInstance         = hInstance;
  31.     wc.hIcon         = LoadIcon( hInstance, MAKEINTRESOURCE(TBICON) );
  32.     wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  33.     wc.hbrBackground     = GetStockObject(WHITE_BRUSH);
  34.     wc.lpszMenuName     = MAKEINTRESOURCE(TBMENU);
  35.     wc.lpszClassName     = "ESDemoTools";
  36.  
  37.  
  38.     return (RegisterClass(&wc));
  39. }
  40.  
  41.  
  42. /************************************************************************/
  43. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  44. {
  45.     HWND    hWnd;
  46.  
  47.     hInst = hInstance;
  48.  
  49.     hWnd = CreateWindow(
  50.         "ESDemoTools",    // See RegisterClass() call.
  51.         "ES Toolbar Demo",    // Text for window title bar.
  52.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,    // Window style.
  53.         CW_USEDEFAULT,            // Default horizontal position.
  54.         CW_USEDEFAULT,            // Default vertical position.
  55.         CW_USEDEFAULT,            // Default width.
  56.         CW_USEDEFAULT,            // Default height.
  57.         NULL,        
  58.         NULL,        
  59.         hInstance,    
  60.         NULL        
  61.     );
  62.  
  63.     if (!hWnd)
  64.         return (FALSE);
  65.  
  66.     ShowWindow(hWnd, nCmdShow);    // Show the window
  67.     UpdateWindow(hWnd);        // Sends WM_PAINT message
  68.     return (TRUE);        // Returns the value from PostQuitMessage
  69.  
  70. }
  71.  
  72.  
  73. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT message,
  74.                      WPARAM wParam, LPARAM lParam )
  75. {
  76.         static HWND hwndTBar;
  77.     switch (message)
  78.     {
  79.  
  80.     case WM_CREATE:
  81.     {
  82.        POINT pnt;
  83.            pnt.x=pnt.y=0;
  84.        hwndTBar=CreateToolBar( hInst, (LPSTR)MAKEINTRESOURCE(TOOLBAR), hWnd, pnt );
  85.        if( !hwndTBar )
  86.        {
  87.           MessageBox( hWnd, "Could not create Tool Bar window", NULL, MB_OK );
  88.           PostMessage( hWnd, WM_CLOSE, 0, 0L );
  89.            }
  90.            break;
  91.         }
  92.  
  93.     case WM_COMMAND:
  94.  
  95.     /* The following WM_COMMAND message processing was NOT intended to be
  96.        nicely written and serves as an example only to show the abilities
  97.        of the Tool Bar control. */
  98.         
  99.        switch( wParam )
  100.            {
  101.  
  102.           case ID_CMD1:
  103.           case ID_CMD2:
  104.           case ID_CMD3:
  105.           case ID_CMD4:
  106.           case ID_CMD5:
  107.               {
  108.        /* This simply prints the wParam of the WM_COMMAND message
  109.           which is equal to the ID number of the pressed TB button
  110.            */
  111.              HDC hdc;
  112.                  char buffer[64];
  113.              hdc = GetDC( hWnd );
  114.          wsprintf( buffer,"Message WM_COMMAND, wParam=0x%X (%u), button # %d",
  115.             wParam, wParam, GetButtonNumber( hwndTBar, wParam ) );
  116.              TextOut( hdc, 10, 20, buffer, lstrlen( buffer ) );
  117.              ReleaseDC( hWnd, hdc );
  118.          break;
  119.           }
  120.           case 1100:
  121.           case 1200:
  122.           case 1300:
  123.           case 1400:
  124.           case 1500:
  125.           /* Toggle disable/enable button */
  126.           {
  127.          int button=(wParam-1100)/100;
  128.          HMENU hm;
  129.          hm=GetMenu( hWnd );
  130.          CheckMenuItem( hm, wParam,
  131.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  132.                     MF_UNCHECKED:MF_CHECKED );
  133.          ES_TB_Toggle_Enable_Disable_Demo( hwndTBar, button );
  134.          // This function actually calls SendMessage API which sends
  135.          // a message to hwndTBar window to change the corresponding
  136.          // style (explanation and source for these functions is sent to registred
  137.          // users only).
  138.          // 
  139.          // Of course you can try to 'Spy' the messages. But I do
  140.          // not think the effort would be worth $20. Anyway, you are
  141.          // not allowed to use it in your programs unless you are
  142.          // registered.
  143.           }
  144.           break;
  145.  
  146.           case 1101:
  147.           case 1201:
  148.           case 1301:
  149.           case 1401:
  150.           case 1501:
  151.           /* Toggle standard/auto 2 state button */
  152.           {
  153.          int button=(wParam-1100)/100;
  154.          HMENU hm;
  155.          hm=GetMenu( hWnd );
  156.          CheckMenuItem( hm, wParam,
  157.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  158.                     MF_UNCHECKED:MF_CHECKED );
  159.          ES_TB_Toggle_Standard_Auto2State_Demo( hwndTBar, button );
  160.          // you need to register to get the documentation on this
  161.          // option. Read the explanation above.
  162.           }
  163.           break;
  164.  
  165.           case 1102:
  166.           case 1202:
  167.           case 1302:
  168.           case 1402:
  169.           case 1502:
  170.           /* Toggle standard/2 state button */
  171.           {
  172.          int button=(wParam-1100)/100;
  173.          HMENU hm;
  174.          hm=GetMenu( hWnd );
  175.          CheckMenuItem( hm, wParam,
  176.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  177.                     MF_UNCHECKED:MF_CHECKED );
  178.          ES_TB_Toggle_Standard_2State_Demo( hwndTBar, button );
  179.          // you need to register to get the documentation on this
  180.          // option. Look up there εεε
  181.          break;
  182.           }
  183.           case 2001:
  184.           case 2002:
  185.           case 2003:
  186.           case 2004:
  187.           case 2005:
  188.           /*
  189.          This function calls SendMessage which sends the corresponding
  190.          message to hwndTBar to set the number of buttons per row.
  191.          Althogh you could specify 0 in resource header to get horizontal
  192.          tool bar, here you have to supply the actual number, 0 does not
  193.          work - it will be ignored
  194.           */
  195.          ES_TB_Set_Number_of_Controls_Demo( hwndTBar, wParam-2000 ); //Register!
  196.          ShowWindow( hwndTBar, SW_SHOW );
  197.           /*
  198.          Call to this and following functions will hide the TB from
  199.          the screen. You need to show the window explicitly. I removed
  200.          this call to ShowWindow from DLL to give a programmer the
  201.          possibility to move the window after it's style was changed before
  202.          the window is displayed. Thus you can override the default
  203.                  positions of window and so on.
  204.                */
  205.          break;
  206.                /*
  207.           case 2006:
  208.          I want to point out that there is no example on something
  209.          like
  210.          ES_TB_Toggle_Popup_Child_Demo( hwndTBar );
  211.          It is not because it is undocumented but because it is
  212.          not implemented. This simply does not work, Windows 3.x
  213.          cannot convert WS_POPUP to WS_CHILD and back. Or at least I
  214.          can say I do not know about this possibility. If you want
  215.          this, you need to destroy the TB with one style and create
  216.                  with another. Or tell me how to make it work.
  217.               */
  218.           case 2007:
  219.           {
  220.                  /* Toggles caption/no caption TB */
  221.          HMENU hm;
  222.          hm=GetMenu( hWnd );
  223.          CheckMenuItem( hm, wParam,
  224.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  225.                     MF_UNCHECKED:MF_CHECKED );
  226.  
  227.          ES_TB_Toggle_Movable_Fixed_Demo( hwndTBar ); //Register!
  228.          ShowWindow( hwndTBar, SW_SHOW );
  229.                  break;
  230.           }
  231.           case 2008:
  232.           {
  233.                  /* Toggles border/no border TB styles */
  234.          HMENU hm;
  235.          hm=GetMenu( hWnd );
  236.          CheckMenuItem( hm, wParam,
  237.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  238.                     MF_UNCHECKED:MF_CHECKED );
  239.  
  240.          ES_TB_Toggle_BorderStyle_Demo( hwndTBar );   //Register!
  241.          ShowWindow( hwndTBar, SW_SHOW );
  242.                  break;
  243.           }
  244.  
  245.           default:;
  246.        }
  247.        break;
  248.     case WM_CLOSE:
  249.        DestroyWindow(hWnd);
  250.        break;
  251.  
  252.     case WM_QUIT:
  253.     case WM_DESTROY:
  254.        PostQuitMessage(0);
  255.        break;
  256.  
  257.     /*
  258.        The following two message control the style of TB caption
  259.        It is intended to be active always when the parent is active.
  260.        It cannot be done without a cooperation from parent's side.
  261.        It may have some bugs. Try, if you have problems, contact me.
  262.     */  
  263.     case WM_NCACTIVATE:
  264.     /* The following is required in order to prevent DefWindowProc
  265.        from redrawing the caption bar of this window in inactive style
  266.        when tool bar window is activated. Try to remove this and you
  267.        will see caption bar flashing when you click in TB window.
  268.        Whenever TB becomes active it passes the active state to it's
  269.        parent and becomes inactive, to look active it redraws it's caption
  270.            in the active style.
  271.     */
  272.        if( (HWND)LOWORD(lParam)==hwndTBar )
  273.           return TRUE;
  274.  
  275.     case WM_ACTIVATEAPP:
  276.     /* This is necessary to draw a title bar of TB in inactive style
  277.        when the application becomes inactive. TB is never active and cannot
  278.        process this message by itself. If you remove this part TB will
  279.        remain looking 'active' even if another application is activated */
  280.        if( !wParam )
  281.           SendMessage( hwndTBar, WM_NCACTIVATE, FALSE, 0L );
  282.        else SendMessage( hwndTBar, WM_NCACTIVATE, TRUE, 0L );
  283.        //break;  -- DO NOT PUT IT HERE. Documentation states incorrectly that
  284.        // you can return 0 if you process this message. You cannot, if you
  285.        // return FALSE you prevent another application from activation.
  286.        // If you want you may try to return wParam for default processing.
  287.     default:
  288.        return (DefWindowProc(hWnd, message, wParam, lParam));
  289.    }
  290.    return (NULL);
  291. }
  292.  
  293. #pragma argsused
  294. /**************************************************************/
  295. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  296.              LPSTR lpCmdLine, int nCmdShow)
  297. {
  298.     MSG msg;        
  299.     if( !hPrevInstance )    
  300.        if (!InitApplication(hInstance))
  301.         return (FALSE);    
  302.  
  303.     if (!InitInstance(hInstance, nCmdShow))
  304.        return (FALSE);
  305.  
  306.     while( GetMessage( &msg, NULL, NULL, NULL ) )    
  307.     {
  308.         TranslateMessage(&msg);    
  309.         DispatchMessage(&msg);    
  310.     }
  311.     return (msg.wParam);
  312. }
  313.  
  314.  
  315.